package com.geekhalo.lego.plugin.aggmethod;

import com.geekhalo.lego.plugin.support.template.CreateClassContext;

import static com.geekhalo.lego.plugin.support.util.Utils.getType;

public class CommandServiceTemplate {
    private static final String TEMPLATE =
            "package {pkg};\n" +
            "\n" +
            "import com.geekhalo.lego.core.command.CommandService;\n" +
            "import com.geekhalo.lego.core.command.CommandServiceDefinition;\n" +
            "import com.geekhalo.lego.core.command.CommandMethodDefinition;\n" +
            "\n" +
            "import {aggTypeFull};\n" +
            "import {repositoryFullType};\n" +
            "\n" +
            "import {commandFullType};\n" +
            "import {contextFullType};\n" +
            "import {resultFullType};\n" +
            "\n" +
            "\n" +
            "@CommandServiceDefinition(domainClass = {aggType}.class, repositoryClass = {repositoryType}.class)\n" +
            "public interface {className}\n" +
            "    extends CommandService<{commandType}, {resultType}> {\n" +
            "\n" +
            "    @Override\n" +
            "    @CommandMethodDefinition(contextClass = {contextType}.class)\n" +
            "    {resultType} execute({commandType} command);\n" +
            "}\n";


    public static String createDomainService(CreateCommandServiceContext context){
        String content = TEMPLATE;
        content = content.replace("{pkg}", context.getPkg());
        content = content.replace("{className}", context.getClsName());
        content = content.replace("{aggType}", context.getAggType());
        content = content.replace("{aggTypeFull}", context.getAggTypeFull());

        content = content.replace("{repositoryType}", context.getRepositoryType());
        content = content.replace("{repositoryFullType}", context.getRepositoryTypeFull());

        content = content.replace("{commandType}", context.getCommandType());
        content = content.replace("{commandFullType}", context.getCommandFullType());

        content = content.replace("{contextType}", context.getContextType());
        content = content.replace("{contextFullType}", context.getContextFullType());

        content = content.replace("{resultType}", context.getResultType());
        content = content.replace("{resultFullType}", context.getResultFullType());
        return content;
    }

    public static class CreateCommandServiceContext extends CreateClassContext{
        private String commandType;
        private String commandFullType;

        private String contextType;
        private String contextFullType;

        private String resultType;
        private String resultFullType;


        public CreateCommandServiceContext(String pkg, String clsName) {
            super(pkg, clsName);
        }

        public void setCommandTypeFull(String commandFullType){
            this.commandFullType = commandFullType;
            this.commandType = getType(commandFullType);
        }

        public void setContextTypeFull(String contextFullType){
            this.contextFullType = contextFullType;
            this.contextType = getType(contextFullType);
        }

        public void setResultTypeFull(String resultFullType){
            this.resultFullType = resultFullType;
            this.resultType = getType(resultFullType);
        }

        public String getCommandType() {
            return commandType;
        }

        public String getCommandFullType() {
            return commandFullType;
        }

        public String getContextType() {
            return contextType;
        }

        public String getContextFullType() {
            return contextFullType;
        }

        public String getResultType() {
            return resultType;
        }

        public String getResultFullType() {
            return resultFullType;
        }
    }
}
